package com.ruoyi.statisticalCenter.controller;

import cn.hutool.core.net.URLEncoder;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.domain.vo.TableVo;
import com.ruoyi.common.service.TableService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.cache.CustomCacheFilter;
import com.ruoyi.hqfinance.domain.HqNetworkBillDetail;
import com.ruoyi.logistic.domain.PageDate;
import com.ruoyi.logistic.domain.PageInfo;
import com.ruoyi.logistic.utils.TableUtils;
import com.ruoyi.statisticalCenter.domain.ScOutReturnSheet;
import com.ruoyi.statisticalCenter.domain.ScQualityControlDisposition;
import com.ruoyi.statisticalCenter.domain.ScQualityControlFeeSheet;
import com.ruoyi.statisticalCenter.domain.vo.*;
import com.ruoyi.statisticalCenter.service.ScQualityControlFeeSheetService;
import com.ruoyi.statisticalCenter.utils.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;

@RestController
@RequestMapping("/sc/qualityControlFee")
@Api(tags = "统计中心-质控费用汇总报表")
public class ScQualityControlFeeSheetController {
    @Autowired
    private ScQualityControlFeeSheetService scQualityControlFeeSheetService;
    @Autowired
    private HttpServletResponse response;
    @Autowired
    private TableService tableService;
    @Autowired
    private RedisUtil redisUtil;

    //@CustomCacheFilter
    @ApiOperation("汇总")
    @PostMapping("/collect")
    public R collect(@RequestBody ScQualityControlFeeSheetVo vo) {
        if (vo != null) {
            String startTime = vo.getStartTime().replace("-", "");
            String endTime = vo.getEndTime().replace("-", "");
            vo.setStartTime(startTime);
            vo.setEndTime(endTime);
        }
        /*String list = scQualityControlFeeSheetService.selectAll().toString();
        // 构建Redis键
        String redisKey = "sc_quality_control_fee_sheet:collect:" + vo.getStartTime() + ":" + vo.getEndTime()
                + ":" + vo.getNetworkLevel() + list + vo.getPageDate().getPageNum() + vo.getPageDate().getPageSize();*/
        // 检查Redis中是否存在数据
        /*R redisResult = redisUtil.getObject(redisKey, R.class);
        if (redisResult != null) {
            return redisResult; // 如果Redis中存在数据，则直接返回
        }*/
        // 如果Redis中不存在数据，则从数据库中查询
        R databaseResult = scQualityControlFeeSheetService.collect(vo);
        // 将数据库查询结果存储到Redis，并设置过期时间为一天
        /*redisUtil.setObject(redisKey, databaseResult, 600);*/
        return databaseResult;
    }

    @ApiOperation("cache")
    @PostMapping("cache")
    public R cache(@RequestBody DateStrVo dateStrVo) {
        return scQualityControlFeeSheetService.cache(dateStrVo);
    }

    @ApiOperation("查询质控费用汇总配置")
    @PostMapping("/search")
    public R search(@RequestBody ScQualityControlDispositionVo vo) {
        ScQualityControlDisposition scQualityControlDisposition = scQualityControlFeeSheetService.search(vo);
        if (scQualityControlDisposition == null) {
            return R.fail("暂无数据");
        } else {
            return R.ok(scQualityControlDisposition);
        }
    }

//    @ApiOperation("添加缓存信息")
//    @GetMapping("/cache")
//    public R cache() {
//
//        return scQualityControlFeeSheetService.cache();
//    }

    @ApiOperation("修改配置")
    @PostMapping("/update")
    public R update(@RequestBody ScQualityControlDisposition scQualityControlDisposition) {
        return R.ok(scQualityControlFeeSheetService.update(scQualityControlDisposition));
    }

    @ApiOperation("导出")
    @PostMapping("/export")
    public void export(@RequestBody ScQualityControlFeeSheetVo vo) {
        if (vo != null) {
            String startTime = vo.getStartTime().replace("-", "");
            String endTime = vo.getEndTime().replace("-", "");
            vo.setStartTime(startTime);
            vo.setEndTime(endTime);
            PageDate pageDate = new PageDate();
            pageDate.setPageNum(1);
            pageDate.setPageSize(10000);
            vo.setPageDate(pageDate);
        }
        String fileName = "attachment;filename=" + URLEncoder.createDefault().encode("质控费用汇总报表", Charset.forName("utf-8")) + ".xlsx";
        response.setHeader("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", fileName);
        response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
//        List<ScQualityControlFeeSheet> list = scQualityControlFeeSheetService.collectExport(vo);
//        ExcelUtil<ScQualityControlFeeSheet> util = new ExcelUtil(ScQualityControlFeeSheet.class);
//        util.exportExcel(response, list, "质控费用汇总报表");
        ServletOutputStream outputStream = null;
        try {
            outputStream = response.getOutputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        scQualityControlFeeSheetService.collectExport(vo, outputStream);
    }

    @ApiOperation("点击跳转")
    @PostMapping("/point")
    public R point(@RequestBody ScQualityControlFeeSheetPointVo vo) {
        if (vo != null) {
            String startTime = vo.getStartTime().replace("-", "");
            String endTime = vo.getEndTime().replace("-", "");
            vo.setStartTime(startTime);
            vo.setEndTime(endTime);
        }
        List<HqNetworkBillDetail> list = scQualityControlFeeSheetService.point(vo);
        List<TableVo> fieldList = tableService.getFieldAnnotation(new HqNetworkBillDetail());
        return TableUtils.handleListResponse(list, fieldList, "暂无数据");
    }


}
